$().ready(function() {
	initDictDatas();
	validateRule();
	laydate.render({
		elem: '#startValidData',
		trigger: 'click',
		isInitValue: false
	});
	laydate.render({
		elem: '#endValidData',
		trigger: 'click',
		isInitValue: false
	});
});
//构建字典数据
function initDictDatas(){
	
	var thatDictFields = window.parent.dictFields;
	var thatDictkeys = window.parent.dictKeys;
	var thatDictDatas = window.parent.dicDatas;
	
	$('#platId').bootstrapSelect({
		url : ctx+'plat/platInfo/listPlatDict',
	    valueField: 'platId',
	    textField: 'platName',
	    placeholder: "请选择所属平台",
	    onChange:function(value){
	    	if(value == null || value == ''){
	    		return false;
	    	}
	    	//级联商户
	    	$('#merchantId').bootstrapSelect({
	    		url:ctx+'plat/merchantInfo/listMerchantDict/'+value,
	    		valueField: 'merchantId',
	    		textField: 'merchantName',
	    		placeholder: "请选择所属商户"
	    	});
	    	//bootstrap-select使用规则是先执行操作然后refresh
	    	$('#merchantId').selectpicker('refresh');
	    }
	});
	$('#cpTypeId').bootstrapSelect({
		url : ctx+'plat/cpType/listCpTypeDict',
		valueField: 'typeId',
		textField: 'typeName',
		placeholder: "请选择优惠券类型"
	});
	$('#oilStationId').bootstrapSelect({
		url : ctx+'plat/shopInfo/listShopDict/10',
		valueField: 'stationId',
		textField: 'stationName',
		placeholder: "请选择派券加油站"
	});
	$('#merchantId').bootstrapSelect({
		valueField: 'merchantId',
		textField: 'merchantName',
		placeholder: "请选择所属商户"
	});
	$('#searchTypeId').bootstrapSelect({
		data:thatDictDatas['cpSearchType'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择搜索分类"
	});
	$('#expiredConf').bootstrapSelect({
		data:thatDictDatas['expiredConf'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择过期处理方式"
	});
	$('#obtainType').bootstrapSelect({
		data:thatDictDatas['obtainType'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择获取类型"
	});
	//默认选择项
//	$('#platId').bootstrapSelect('setValue',10);
//	$('#merchantId').bootstrapSelect('setValue',10);
//	$('#cpTypeId').bootstrapSelect('setValue',14);
//	$('#searchTypeId').bootstrapSelect('setValue',10);
	$('#expiredConf').bootstrapSelect('setValue',1);
}
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	//TODO 校验至少输入第1条使用须知
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"coupon/cpInfo/save",
		data : $('#signupForm').serialize(),// 你的formid
		dataType: 'json',
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			cpName : {
				required : true,
				maxlength: 12
			},
			oilCpNum : {
				required : true,
			},
			cpTypeId : {
				required : true,
			},
			searchTypeId : {
				required : true,
			},
			platId : {
				required : true,
			},
			merchantId : {
				required : true,
			},
			expiredConf : {
				required : true,
			},
			startValidData : {
				required : true,
			},
			endValidData : {
				required : true,
			},
			cpNum : {
				required : true,
				isPositiveInt:true,
				/*max: 99999,*/
				min: 1
			},
			payFee : {
				required : true,
				number:true,
				max: 99999,
				min: 0
			},
			costFee : {
				required : true,
				number:true,
				max: 99999,
				min: 0
			},
			subsidyFee : {
				required : true,
				number:true,
				max: 99999,
				min: 0
			},
			servFee : {
				required : true,
				number:true,
				max: 99999,
				min: 0
			}
		},
		messages : {
			cpName : {
				required : icon + "请填写优惠券名称,最多12个字符"
			},
			oilCpNum : {
				required : icon + "请输入券商品编码"
			},
			cpTypeId : {
				required : icon + "请选择券类型"
			},
			searchTypeId : {
				required : icon + "请选择券搜索分类"
			},
			platId : {
				required : icon + "请选择所属平台"
			},
			merchantId : {
				required : icon + "请选择所属商户"
			},
			expiredConf : {
				required : icon + "请选择过期处理方式"
			},
			startValidData : {
				required : icon + "请设置有效期开始时间"
			},
			endValidData : {
				required : icon + "请设置有效期结束时间"
			},
			cpNum : {
				required : icon + "请输入优惠券库存数量",
				/*max: icon + "请输入小于9999的正整数",*/
				min: icon + "请输入大于0的正整数"
			},
			payFee : {
				required : icon + "请输入券面价值"
			},
			costFee : {
				required : icon + "请输入购买价格"
			},
			subsidyFee : {
				required : icon + "请输入补贴金额"
			},
			servFee : {
				required : icon + "请输入手续费"
			}
		},
		errorPlacement: function(error, element) {
			if (element[0].tagName && element[0].tagName.toLowerCase() == 'select'){
				error.appendTo(element.parent().parent());
			}else {					
				error.appendTo(element.parent());
			}
		}
	})
}